package com.xnxy.design.action;


import com.xnxy.design.model.Goods;
import com.xnxy.design.model.User;
import com.xnxy.design.service.CoreService;
import com.xnxy.design.service.GoodsService;
import com.xnxy.design.util.PageData;
import com.xnxy.design.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsAction extends  CoreController<Goods,Integer>{

    @Autowired
    GoodsService goodsService;

    @Override
    public CoreService<Goods, Integer> getService() {
        return goodsService;
    }


    @RequestMapping("/index")
    private ModelAndView getTop(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("index");
        PageData pageData = queryData(request);
        modelAndView.addObject("list", pageData.getListData());
        return modelAndView;
    }

    @RequestMapping("/goods/ry")
    private ModelAndView htmlRy(Integer id) {
        Goods goods=selectByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView("ry");
        modelAndView.addObject("goods",goods);
        modelAndView.addObject("type",1);
        return modelAndView;
    }

    @RequestMapping("/admin/goods/getGoods")
    @ResponseBody
    public Map<String,Object> getUser(Integer id){
        Goods goods=selectByPrimaryKey(id);
        return success(true,"请求成功",goods);
    }


    @RequestMapping("/admin/goods/inster")
    @ResponseBody
    public Map<String,Object> inster(Goods goods,MultipartFile file) throws IOException {
        if (file!=null){
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            String destFileName = SessionUtil.gerReq().getServletContext().getRealPath("") + "static" + File.separator + fileName;
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            //5.把浏览器上传的文件复制到希望的位置
            file.transferTo(destFile);
            goods.setImg("static/"+fileName);
        }
        goods.setTime(new Date());
        if (goods.getId()==null){
            goods.setUserid(SessionUtil.getUser().getId());
            insert(goods);
        }else {
            updata(goods);
        }
        return success(true,"请求成功");
    }


    @RequestMapping("/admin/goods/list")
    @ResponseBody
    private Map<String,Object> goodsAll(){
        List list=selectAll();
        return success(true,"请求成功",list);
    }


    @RequestMapping("/admin/goods/delect")
    @ResponseBody
    public Map<String,Object> delectGoods(Integer id[]){
        Boolean flag=delect(id);
        if (flag){
            return success(true,"删除成功");
        }else {
            return success(false,"删除失败");
        }

    }
}
